package com.hs.controller;

import com.hs.entity.TbUser;
import com.hs.service.ClassService;
import com.hs.service.MenuService;
import com.hs.service.StudentService;
import com.hs.service.UserService;
import com.hs.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    @Autowired
    private ClassService classService;

    @Autowired
    private StudentService studentService;

    @RequestMapping("/initMenu")
    @ResponseBody
    public Map initMenu(){
        TbUser user=ShiroUtils.getSessionUser();
        //调用菜单生成方法
        Map<String,Object> menuMap = menuService.getMenus(user.getRoleid());
        //将数据返回给客户端
        /*JSON.toJSONString(menuMap);*/
        return menuMap;
    }

    @RequestMapping("/welcome")

    private String welcome(Model model) throws SQLException {
        /*通过shiro获取权限*/
        Subject subject= SecurityUtils.getSubject();
        TbUser user= (TbUser) subject.getPrincipal();
        Session shiroSession= subject.getSession();
        TbUser tbUser = (TbUser) shiroSession.getAttribute("user");
        /*获取主体的身份信息，判断，并返回不同的首页*/
        if (tbUser.getRoleid()==1){
            /*查找数据*/
            String data_manager= String.valueOf(userService.countUser("1"));
            String data_teacher= String.valueOf(userService.countUser("2"));
            String data_student= String.valueOf(userService.countUser("3"));
            model.addAttribute("data_manager",data_manager);
            model.addAttribute("data_student",data_student);
            model.addAttribute("data_teacher",data_teacher);
            return "manager/welcome";
        }else if (tbUser.getRoleid()==2){

            return "teacher/welcome";
        }else {
            /*查找数据*/
            String studentId = String.valueOf(tbUser.getId());
            /*课程考试成绩数*/
            String data_exam= String.valueOf(studentService.countExamTime(String.valueOf(studentId)));
            /*考试均分*/
            Double data_avg_str= studentService.calAverageScore(String.valueOf(studentId));
            /*考试不及格常场数*/
            String data_fail= String.valueOf(studentService.countFailTime(String.valueOf(studentId)));
            /*总课程数*/
            String data_allExam= String.valueOf(studentService.countTotalTime());
            /*全体课程数*/
            Double data_allAvg_str= studentService.calAllAverageScore();
            /*格式化数据*/
            String data_avg = String.format("%.2f", data_avg_str);
            String data_allAvg = String.format("%.2f", data_allAvg_str);
            /*利用model传值，并返回页面*/
            model.addAttribute("data_exam",data_exam);
            model.addAttribute("data_avg",data_avg);
            model.addAttribute("data_fail",data_fail);
            model.addAttribute("data_allExam",data_allExam);
            model.addAttribute("data_allAvg",data_allAvg);
            return "student/welcome";
        }
    }

}

